<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="dialogForm">新增</el-button>
    </div>

    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label='ID' width="95">
        <template slot-scope="scope">
          {{scope.row.id}}
        </template>
      </el-table-column>
      <el-table-column label="名称">
        <template slot-scope="scope">
          {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column label="文章数量" align="center">
        <template slot-scope="scope">
          {{scope.row.count}}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | typeFilter">{{scope.row.status| statusFilter}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" align="center">
        <template slot-scope="scope">
          <el-button :type="scope.row.status | changeStatusTypeFilter" size="mini" icon="el-icon-refresh" @click="changeStatus(scope.row)">{{scope.row.status| changeStatusFilter}}
          </el-button>
          <el-button type="info" size="mini" icon="el-icon-edit"  @click="dialogForm(scope.row)">修改</el-button>
          <el-button type="warning" size="mini" icon="el-icon-delete" @click="deleteRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page.sync="page.currentPage"
        :total="page.total"
        @current-change="fetchData">
      </el-pagination>
    </div>

    <el-dialog :title="dialogFormTitle" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="标签ID" :label-width="formLabelWidth" v-if="isMod">
          <el-input v-model="form.id" auto-complete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="标签名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="subForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getList, changeStatus, deleteRow, editInfo } from '@/api/tag'

  export default {
    data() {
      return {
        list: null,
        listLoading: true,
        dialogFormVisible: false,
        dialogFormTitle: '新增分类',
        formLabelWidth: '120px',
        isMod: false,
        form: {
          id: '',
          name: ''
        },
        page: {
          total: 0,
          currentPage: 1
        }
      }
    },
    filters: {
      typeFilter(status) {
        const statusMap = {
          0: 'success',
          1: 'danger'
        }
        return statusMap[status]
      },
      statusFilter(status) {
        const statusMap = {
          0: '启用',
          1: '禁用'
        }
        return statusMap[status]
      },
      changeStatusTypeFilter(status) {
        const statusMap = {
          1: 'success',
          0: 'danger'
        }
        return statusMap[status]
      },
      changeStatusFilter(status) {
        const statusMap = {
          1: '启用',
          0: '禁用'
        }
        return statusMap[status]
      }
    },
    created() {
      this.fetchData(1)
    },
    methods: {
      fetchData(pageNo) {
        this.listLoading = true
        getList({ 'pageNumber': pageNo }).then(response => {
          this.list = response.data.content
          this.listLoading = false
          this.page.total = response.data.totalElements
        })
      },
      changeStatus(row) {
        this.listLoading = true
        changeStatus(row.id).then(response => {
          if (response.code === 0) {
            this.$message({ message: '操作成功', type: 'success' })
            this.fetchData(this.page.currentPage)
            this.listLoading = false
          } else {
            this.$message.error('操作失败' + response.message)
          }
        })
      },
      deleteRow(row) {
        this.listLoading = true
        deleteRow(row.id).then(response => {
          if (response.code === 0) {
            this.$message({ message: '删除成功', type: 'success' })
            this.fetchData(this.page.currentPage)
            this.listLoading = false
          } else {
            this.$message.error('删除失败' + response.message)
          }
        })
      },
      dialogForm(row) {
        // 用来控制是否要显示ID，如果ID存在，则证明是修改，需要展示只读的ID输入框
        if (row.id !== undefined) {
          this.isMod = true
          this.dialogFormTitle = '标签修改'
        } else {
          this.isMod = false
          this.dialogFormTitle = '标签新增'
        }
        this.form.id = row.id
        this.form.name = row.name
        this.form.status = row.status
        this.dialogFormVisible = true
      },
      subForm() {
        this.listLoading = true
        editInfo(this.form).then(response => {
          if (response.code === 0) {
            this.$message({ message: '提交成功', type: 'success' })
            this.fetchData(this.page.currentPage)
            this.listLoading = false
            this.dialogFormVisible = !this.dialogFormVisible
          } else {
            this.$message.error('提交失败' + response.message)
          }
        })
      }
    }
  }
</script>
